<?php
class Questions_model extends CI_Model
{
    var $collection = 'questions';
    
    public function __construct()
	{
		parent::__construct();
        $this->load->library('cimongo/cimongo', '', 'mongo');
	}
    
    public function getQuestions($limit, $offset = 0)
    {
        $limit = (int) $limit;
        $offset = (int) $offset;
        
        $data = $this->mongo
            ->order_by(array('create_time' => 'DESC'))
            ->get($this->collection, $limit, $offset);
        
        return $data->result();
    }
    
    public function getQuestionsByDays($days = 0, $limit = 500)
    {
        $days = ($days < 0) ? 0 : (int) $days;
        $limit = (int) $limit;
        $startDay = strtotime(date("Y-m-d")) - $days * 86400;
        $endDay = $startDay + 86399;
        
        $data = $this->mongo
            ->where(array('create_time' => array('$gte' => $startDay, '$lte' => $endDay)))
            ->order_by(array('create_time' => 'DESC'))
            ->limit($limit)
            ->get($this->collection);
        
        return $data->result();
    }
}